#ifndef SQLPARSER_STATEMENTS_H
#define SQLPARSER_STATEMENTS_H

#include "statSelect.h"
#include "statImport.h"
#include "statCreate.h"
#include "statInsert.h"
#include "statUpdate.h"
#include "statDelete.h"
#include "statDrop.h"
#include "statPrepare.h"
#include "statExecute.h"
#include "statShow.h"
#include "statUse.h"
#include <memory>
#include <string>
#include <vector>
#include <unordered_map>
#include <functional>

namespace ment {

    // 前向声明
    class SQLStatementContext;
    class SQLStatementFactory;
    class SQLStatementProcessor;
    class SQLStatementValidator;
    class SQLExecutionEngine;

    // SQL语句处理配置
    struct SQLStatementConfig {
        bool enableValidation = true;
        bool enableOptimization = false;
        bool enableLogging = true;
        int maxExecutionTime = 30000; // 毫秒
        std::string defaultSchema = "";
    };

    // SQL语句处理接口
    class ISQLStatementProcessor {
    public:
        virtual ~ISQLStatementProcessor() = default;
        virtual std::string execute(const std::string& username, SQLStatement* statement) = 0;
        virtual bool validate(SQLStatement* statement) = 0;
        virtual void optimize(SQLStatement* statement) = 0;
    };

    // SQL语句处理的管理器
    class SQLStatementContext {
    public:
        SQLStatementContext();
        ~SQLStatementContext();

        void setConfig(const SQLStatementConfig& config);
        const SQLStatementConfig& getConfig() const;

        void setCurrentUser(const std::string& username);
        const std::string& getCurrentUser() const;

        void setCurrentSchema(const std::string& schema);
        const std::string& getCurrentSchema() const;

        void addExecutionLog(const std::string& log);
        const std::vector<std::string>& getExecutionLogs() const;

        void reset();

    private:
        SQLStatementConfig config_;
        std::string currentUser_;
        std::string currentSchema_;
        std::vector<std::string> executionLogs_;
    };

    // SQL语句验证器
    class SQLStatementValidator {
    public:
        SQLStatementValidator(SQLStatementContext* context);

        bool validateStatement(SQLStatement* statement);
        bool validateSyntax(SQLStatement* statement);
        bool validateSemantics(SQLStatement* statement);
        bool validatePermissions(SQLStatement* statement, const std::string& username);

        const std::vector<std::string>& getValidationErrors() const;
        void clearErrors();

    private:
        SQLStatementContext* context_;
        std::vector<std::string> validationErrors_;
    };

    // SQL语句处理器
    class SQLStatementProcessor {
    public:
        SQLStatementProcessor(SQLStatementContext* context);

        std::string processStatement(SQLStatement* statement, const std::string& username);
        void setPreProcessor(std::function<void(SQLStatement*)> processor);
        void setPostProcessor(std::function<void(SQLStatement*, const std::string&)> processor);

    private:
        SQLStatementContext* context_;
        std::unique_ptr<SQLStatementValidator> validator_;
        std::function<void(SQLStatement*)> preProcessor_;
        std::function<void(SQLStatement*, const std::string&)> postProcessor_;
    };

    // SQL执行引擎
    class SQLExecutionEngine {
    public:
        SQLExecutionEngine(const SQLStatementConfig& config = SQLStatementConfig());
        ~SQLExecutionEngine();

        std::string executeStatement(SQLStatement* statement, const std::string& username);
        void setExecutionTimeout(int timeoutMs);
        void enableQueryOptimization(bool enable);

        const SQLStatementContext& getContext() const;

    private:
        std::unique_ptr<SQLStatementContext> context_;
        std::unique_ptr<SQLStatementProcessor> processor_;
    };

    // SQL Statement Factory
    class SQLStatementFactory {
    public:
        static SQLStatementFactory& getInstance();

        std::unique_ptr<ISQLStatementProcessor> createProcessor(const SQLStatementConfig& config = SQLStatementConfig());
        std::unique_ptr<SQLExecutionEngine> createExecutionEngine(const SQLStatementConfig& config = SQLStatementConfig());

        void registerStatementType(StatementType type, std::function<std::unique_ptr<SQLStatement>()> factory);
        std::unique_ptr<SQLStatement> createStatement(StatementType type);

    private:
        SQLStatementFactory() = default;
        std::unordered_map<StatementType, std::function<std::unique_ptr<SQLStatement>()>> statementFactories_;
    };

    // 默认SQL语句处理器实现
    class DefaultSQLStatementProcessor : public ISQLStatementProcessor {
    public:
        DefaultSQLStatementProcessor(const SQLStatementConfig& config = SQLStatementConfig());
        ~DefaultSQLStatementProcessor();

        std::string execute(const std::string& username, SQLStatement* statement) override;
        bool validate(SQLStatement* statement) override;
        void optimize(SQLStatement* statement) override;

    private:
        std::unique_ptr<SQLStatementContext> context_;
        std::unique_ptr<SQLStatementProcessor> processor_;
    };

    class SQLStatementManager {
    public:
        static std::string executeStatement(SQLStatement* statement, const std::string& username);
        static bool validateStatement(SQLStatement* statement);

        // 新的工厂方法
        static std::unique_ptr<ISQLStatementProcessor> createProcessor(const SQLStatementConfig& config = SQLStatementConfig());
        static std::unique_ptr<SQLExecutionEngine> createEngine(const SQLStatementConfig& config = SQLStatementConfig());

        // 全局配置管理
        static void setGlobalConfig(const SQLStatementConfig& config);
        static const SQLStatementConfig& getGlobalConfig();

    private:
        SQLStatementManager() = default;
        static SQLStatementConfig globalConfig_;
        static std::unique_ptr<DefaultSQLStatementProcessor> defaultProcessor_;
    };

} // namespace ment

#endif // SQLPARSER_STATEMENTS_H
